package huoye.shops.huoye.operation.widget;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.ios.dialog.base.BaseDialog;

import huoye.shops.R;


/**
 * Created by uesr on 2018/8/11.
 */

public class SelectPrizeNumDialog extends BaseDialog<SelectPrizeNumDialog> {



    private Context mContext;

    private ImageView ivOut;
    private TextView tvRight;
    private EditText etNum;


    public SelectPrizeNumDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View onCreateView() {
        widthScale(0.75f);
        View inflate = View.inflate(context, R.layout.dialog_select_prize_num, null);
        etNum = (EditText) inflate.findViewById(R.id.dialog_select_prize_num_et);
        tvRight = (TextView) inflate.findViewById(R.id.dialog_select_prize_num_tv_right);
        ivOut = (ImageView) inflate.findViewById(R.id.dialog_select_prize_num_iv_out);
        return inflate;
    }

    @Override
    public void setUiBeforShow() {

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = etNum.getText().toString();
                if(StringUtils.isNotEmpty(s)){
                    int  num = Integer.valueOf(s);
                    if(num == 0){
                        Toast.makeText(mContext,"最小份数必须大于1",Toast.LENGTH_SHORT).show();
                        return;
                    }else if(num > mNum){
                        Toast.makeText(mContext,"不能大于库存",Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        listener.onRightClick(num);
                        dismiss();
                    }
                }
            }
        });

        ivOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   dismiss();
            }
        });
    }



    private int mNum;
    public void setNum(int num){
        mNum = num;
        etNum.setHint("当前库存"+num);
    }

    public interface OnSelectPrizeNumListener {
        void onRightClick(int  num);
    }
    public void setSelectPrizeNumListener(OnSelectPrizeNumListener onListener) {
        this.listener = onListener;
    }

    private OnSelectPrizeNumListener listener;

}
